﻿''' <summary>
''' Form base dùng để hiển thị danh sách dữ liệu
''' </summary>
''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
Public Class frmBaseList

#Region "Property"

    ''' <summary>
    ''' Thuộc tính lưu đối tượng xử lý các thao tác nghiệp vụ
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
    Private m_oBusinessObject As BL.clsBLBase
    Public Property BusinessObject() As BL.clsBLBase
        Get
            Return m_oBusinessObject
        End Get
        Set(ByVal value As BL.clsBLBase)
            m_oBusinessObject = value
        End Set
    End Property

    ''' <summary>
    ''' Biến lưu lại hoạt động hiện tại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 28/06/2011</remarks>
    Private m_eAction As clscommon.ActionMode
    Public Property Action() As clscommon.ActionMode
        Get
            Return m_eAction
        End Get
        Set(ByVal value As clscommon.ActionMode)
            m_eAction = value
        End Set
    End Property
#End Region

#Region "Method"

#Region "Overridable"

    ''' <summary>
    ''' Sub khởi tạo business object
    ''' </summary>
    ''' <param name="connectionString">Chuỗi kết nối đến database</param>
    ''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
    Protected Overridable Sub InitBusinessObject(ByVal connectionString As String)
    End Sub

    ''' <summary>
    ''' Sub lấy thông tin của form
    ''' </summary>
    ''' <returns>Trả lại các form kế thừa từ frmBaseDetail</returns>
    ''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
    Protected Overridable Function GetFormDetail() As frmBaseDetail
        Return Nothing
    End Function

    ''' <summary>
    ''' Sub load dữ liệu vào dataset
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
    Protected Overridable Sub RefreshData()
        FMSDataSet.Clear()
        With BusinessObject
            .dsFMS = Me.FMSDataSet
            .GetData()
        End With
        Dim hasItem As Boolean = FMSBindingSource.Count > 0
        With tbrDataList.Tools
            .Item("Edit").SharedProps.Enabled = hasItem
            .Item("Delete").SharedProps.Enabled = hasItem
            .Item("View").SharedProps.Enabled = hasItem
            .Item("Print").SharedProps.Enabled = hasItem
        End With
    End Sub

    Protected Overridable Sub ViewList()

    End Sub

    Protected Overridable Sub PrintList()

    End Sub
#End Region

    ''' <summary>
    ''' Sub hiển thị form chi tiết dữ liệu
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
    Private Sub ShowFormDetail()
        Dim frmDetail As frmBaseDetail = GetFormDetail()
        With frmDetail
            .Owner = Me
            .ActionMode = Action
            .BusinessObject = Me.BusinessObject
            .bsFMS = Me.FMSBindingSource
            .ShowDialog()
        End With
    End Sub

    ''' <summary>
    ''' Sub xóa bản ghi hiện tại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 24/10/2010</remarks>
    Friend Sub DeleteCurrentRow()
        If Not IsNothing(FMSBindingSource.Current) Then
            Using dlgDeleteValidator As New dlgDeleteValidator() With {.Owner = Me, .BusinessObject = Me.BusinessObject}
                dlgDeleteValidator.lblDeleteValidationMessage.Text = String.Format(My.Resources.DeleteQuestion, BusinessObject.TableName)
                dlgDeleteValidator.ShowDialog()
                If dlgDeleteValidator.DialogResult = Windows.Forms.DialogResult.OK Then
                    If BusinessObject.DeleteCurrentRow(dlgDeleteValidator.txtDeleteReason.Text) = False Then
                        clscommon.ShowException(String.Format(My.Resources.DeleteError, BusinessObject.TableName))
                    End If
                    RefreshData()
                End If
            End Using
        Else
            clscommon.ShowInformation(String.Format(My.Resources.NoRowSeleted, BusinessObject.TableName))
        End If
    End Sub
#End Region

#Region "Event"

    ''' <summary>
    ''' Khi load form thì load danh sách dữ liệu
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 04/06/2011</remarks>
    Private Sub frmBaseList_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            If Not Me.DesignMode Then
                Action = Nothing
                InitBusinessObject((String.Format(My.Resources.ConnectionString, clscommon.DecodingData(My.Settings.Svrnm), clscommon.DecodingData(My.Settings.Svrlgnm), clscommon.DecodingData(My.Settings.Svrpsw))))
                RefreshData()
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi double click một dòng trong datalist thì hiển thị form detail
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 09/06/2011</remarks>
    Private Sub grdDataList_DoubleClickRow(sender As System.Object, e As Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs) Handles grdDataList.DoubleClickRow
        Try
            If Not IsNothing(BusinessObject.CurrentRow) Then
                Action = clscommon.ActionMode.View
                ShowFormDetail()
                Action = Nothing
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Nhấn enter khi con trỏ đang ở một dòng dữ liệu thì hiển thị form detail
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 28/06/2011</remarks>
    Private Sub grdDataList_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles grdDataList.KeyDown
        Try
            If Not IsNothing(BusinessObject.CurrentRow) Then
                If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Space Then
                    Action = clscommon.ActionMode.View
                    ShowFormDetail()
                    Action = Nothing
                ElseIf e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
                    DeleteCurrentRow()
                ElseIf e.KeyCode = Keys.F5 Then
                    RefreshData()
                ElseIf e.KeyCode = Keys.Add Or e.KeyCode = Keys.A Then
                    Action = clscommon.ActionMode.Add
                    ShowFormDetail()
                    Action = Nothing
                ElseIf e.KeyCode = Keys.E Then
                    Action = clscommon.ActionMode.Edit
                    ShowFormDetail()
                    Action = Nothing
                ElseIf e.KeyCode = Keys.P Then
                    PrintList()
                ElseIf e.KeyCode = Keys.V Then
                    ViewList()
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Xác định hành động khi click vào một item trên toolbar
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 24/10/2010</remarks>
    Private Sub tbrDataList_ToolClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinToolbars.ToolClickEventArgs) Handles tbrDataList.ToolClick
        Try
            Select Case e.Tool.Key
                Case "Add"
                    Action = clscommon.ActionMode.Add
                    ShowFormDetail()
                    Action = Nothing
                Case "Edit"
                    If Not IsNothing(FMSBindingSource.Current) Then
                        Action = clscommon.ActionMode.Edit
                        ShowFormDetail()
                        Action = Nothing
                    Else
                        clscommon.ShowInformation(String.Format(My.Resources.NoRowSeleted, BusinessObject.IdentifyObject()))
                    End If
                Case "Delete"
                    DeleteCurrentRow()
                Case ("Refresh")
                    RefreshData()
                Case "View"
                    ViewList()
                Case "Print"
                    PrintList()
            End Select
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi binding source thay đổi giá trị current thì set lại current row
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 24/10/2010</remarks>
    Private Sub FMSBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FMSBindingSource.CurrentChanged
        Try
            If Not IsNothing(FMSBindingSource.Current) Then
                If FMSBindingSource.Current.ToString() <> "System.Data.DataViewManagerListItemTypeDescriptor" And Action <> clscommon.ActionMode.Add Then
                    BusinessObject.CurrentRow = FMSBindingSource.Current.Row
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Thay đổi sort order trên BindingSource khi sort order trên Grid thay đổi
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 21/06/2012</remarks>
    Private Sub grdDataList_AfterSortChange(sender As System.Object, e As Infragistics.Win.UltraWinGrid.BandEventArgs) Handles grdDataList.AfterSortChange
        Try
            Dim SortKey As String = e.Band.SortedColumns.Item(0).Key
            If (FMSBindingSource.Sort = SortKey) Then
                FMSBindingSource.Sort += " DESC"
            Else
                FMSBindingSource.Sort = SortKey
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Select 1 item trên grid khi nhấn chuột phải
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 21/06/2012</remarks>
    Private Sub grdDataList_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles grdDataList.MouseDown
        Try
            If e.Button = Windows.Forms.MouseButtons.Right Then
                Dim ue As Infragistics.Win.UIElement = Me.grdDataList.DisplayLayout.UIElement.ElementFromPoint(New System.Drawing.Point(e.X, e.Y))
                Dim row As Infragistics.Win.UltraWinGrid.UltraGridRow = TryCast(ue.GetContext(GetType(Infragistics.Win.UltraWinGrid.UltraGridRow)), Infragistics.Win.UltraWinGrid.UltraGridRow)
                If row IsNot Nothing Then
                    If Not row.IsActiveRow Then
                        row.Activate()
                    End If
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub
#End Region
End Class